import type { App } from 'vue'
import { Pinia } from 'pinia'
// Pina持久储存
import { createPersistedStatePlugin } from 'pinia-plugin-persistedstate-2'
// import * as localforage from 'localforage'
import stringify from 'json-stringify-safe'

export const setStore = (app: App) => {
  const pinia: Pinia = createPinia()
  // const storage = import.meta.env.DEV ? sessionStorage : localforage
  const storage = sessionStorage
  pinia.use(
    createPersistedStatePlugin({
      storage: {
        getItem: async (key: string) => storage.getItem(key),
        setItem: async (key: string, value: any) => storage.setItem(key, value),
        removeItem: async (key: string) => storage.removeItem(key),
      },
      serialize: (value: any) =>
        // console.log(value)
        // return value && JSON.stringify(value)
        stringify(value),
    })
  )
  app.use(pinia)
}
